<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Department Registration Test</title>
</head>
<body>
<h1>Department Registration Test</h1>
<form id="registrationForm">
    <label for="departmentName">Department Name:</label>
    <input type="text" id="departmentName" name="departmentName" required><br><br>

    <label for="level">Level:</label>
    <select id="level" name="level" required>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select><br><br>

    <button type="submit">Register Department</button>
</form>

<div id="response"></div>

<script>
    document.getElementById('registrationForm').addEventListener('submit', function(event) {
        event.preventDefault();

        const departmentName = document.getElementById('departmentName').value;
        const level = document.getElementById('level').value;

        const data = {
            department_name: departmentName,
            level: level
        };

        fetch('/api/departments/add', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
            .then(response => response.json())
            .then(result => {
                document.getElementById('response').innerHTML = `<pre>${JSON.stringify(result, null, 2)}</pre>`;
            })
            .catch(error => {
                document.getElementById('response').innerHTML = `<pre>Error: ${error}</pre>`;
            });
    });
</script>
</body>
</html>
